package com.hg.library.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.hg.highglass_library.R;

/**
 * Created by admin on 2017/8/14.
 */

public class BuiProgressBar extends ProgressBar {
    private static final String TAG = "BuiProgressBar";
    Bitmap bitmap1, bitmap2, bitmap3, bitmap4;

    StepModel step1Model = new StepModel();
    StepModel step2Model = new StepModel();
    StepModel step3Model = new StepModel();

    public BuiProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.bui_triangle_01);
        bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.bui_triangle_02);
        k1 = bitmap1.getWidth() + 10;
        k2 = bitmap1.getHeight() + 4;

        bitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.bui_triangle_shadow_01);
        bitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.bui_triangle_shadow_02);

    }

    float r;

    public float getInterpolation(float input) {
        return (float) (Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        ViewGroup viewGroup = (ViewGroup) getParent();
        if (viewGroup != null && viewGroup.getClipChildren()){viewGroup.setClipChildren(false);}
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG| Paint.FILTER_BITMAP_FLAG));

        canvas.translate(0, 10);
        canvas.rotate(r -= 1f, getWidth() / 2, getHeight() / 2);

        if (!step1(canvas)) ;
        else if (!step2(canvas)) ;
        else if (!step3(canvas)) ;

    }

    float k1, k2;

    float T = 2f;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

    }

    private boolean step1(final Canvas canvas) {
        step1Model.in += T;
        step1Model.x = getInterpolation(step1Model.in / k1) * k1;

        if (step1Model.in >= k1) {
            step1Model.in = k1;
            return true;
        }

        step1Model.rectF1 = new RectF(step1Model.x, k2, bitmap1.getWidth() + step1Model.x, bitmap1.getHeight() + k2);
        step1Model.rectF2 = new RectF(k1 - step1Model.x, 0, bitmap2.getWidth() + (k1 - step1Model.x), bitmap2.getHeight());

        RectF rectF1 = step1Model.rectF1;
        RectF rectF2 = step1Model.rectF2;

        drawShadow(canvas, rectF1, rectF2);
        canvas.drawBitmap(bitmap1, null, rectF1, null);
        canvas.drawBitmap(bitmap2, null, rectF2, null);
        invalidate();

        return false;
    }

    private boolean step2(Canvas canvas) {

        float n = bitmap1.getHeight() / (bitmap1.getWidth() / 2f);

        float k = k1 / 2f;

        if (step2Model.in >= k) return true;

        step2Model.rectF1 = new RectF(step1Model.rectF1);
        step2Model.rectF2 = new RectF(step1Model.rectF2);

        RectF rectF1 = step2Model.rectF1;
        RectF rectF2 = step2Model.rectF2;

        step2Model.in += T / n;

        float p = getInterpolation(step2Model.in / k) * k;

        rectF1.left = rectF1.left - p;
        rectF1.top = rectF1.top - n * p;
        rectF1.right = rectF1.right - p;
        rectF1.bottom = rectF1.bottom - n * p;

        rectF2.left = rectF2.left + p;
        rectF2.top = rectF2.top + n * p;
        rectF2.right = rectF2.right + p;
        rectF2.bottom = rectF2.bottom + n * p;

        drawShadow(canvas, rectF1, rectF2);
        canvas.drawBitmap(bitmap1, null, rectF1, null);
        canvas.drawBitmap(bitmap2, null, rectF2, null);
        invalidate();

        return false;
    }

    private boolean step3(Canvas canvas) {
        float n = bitmap1.getHeight() / (bitmap1.getWidth() / 2f);

        float k = k1 / 2f;

        if (step3Model.in >= k) {
            RectF rectF1 = step3Model.rectF1;
            RectF rectF2 = step3Model.rectF2;
            rectF1.left = 0;
            step1Model.clear();
            step2Model.clear();
            step3Model.clear();
            drawShadow(canvas, rectF1, rectF2);
            canvas.drawBitmap(bitmap1, null, rectF1, null);
            canvas.drawBitmap(bitmap2, null, rectF2, null);
            postInvalidateDelayed(60);
            return true;
        }

        step3Model.rectF1 = new RectF(step2Model.rectF1);
        step3Model.rectF2 = new RectF(step2Model.rectF2);

        RectF rectF1 = step3Model.rectF1;
        RectF rectF2 = step3Model.rectF2;

        step3Model.in += T / n;

        float p = getInterpolation(step3Model.in / k) * k;

        rectF1.left = rectF1.left - p;
        rectF1.top = rectF1.top + n * p;
        rectF1.right = rectF1.right - p;
        rectF1.bottom = rectF1.bottom + n * p;

        rectF2.left = rectF2.left + p;
        rectF2.top = rectF2.top - n * p;
        rectF2.right = rectF2.right + p;
        rectF2.bottom = rectF2.bottom - n * p;

        drawShadow(canvas, rectF1, rectF2);
        canvas.drawBitmap(bitmap1, null, rectF1, null);
        canvas.drawBitmap(bitmap2, null, rectF2, null);
        invalidate();
        return false;
    }

    private void drawShadow(Canvas canvas, RectF rectF1, RectF rectF2) {
        rectF1 = new RectF(rectF1);
        rectF2 = new RectF(rectF2);

        float v = (float) (6 * Math.cos((r % 360)/ Math.PI/6));
        float b = (float) (6 * Math.sin((r % 360)/ Math.PI/4));

        rectF1.left += v;
        rectF1.right += v;

        rectF2.left += v;
        rectF2.right += v;

        Paint paint1 = new Paint();
        paint1.setColor(0x25000000);

        canvas.drawBitmap(bitmap3, null, rectF1, paint1);
        canvas.drawBitmap(bitmap4, null, rectF2, paint1);
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        params.width = 178;
        params.height = 188;
        super.setLayoutParams(params);
    }


    static class StepModel {
        public float x, in;

        public RectF rectF1;
        public RectF rectF2;

        public void clear() {
            x = 0;
            in = 0;
            rectF1 = null;
            rectF2 = null;
        }
    }

}
